package com.andware.volley.toolbox;

import android.util.Log;

import com.andware.volley.AuthFailureError;
import com.andware.volley.NetworkResponse;
import com.andware.volley.ParseError;
import com.andware.volley.Response;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ishishuji on 14-7-10.
 */
public class GsonRequest < T > extends JsonRequest< T > {
    public static final Gson mGson = new Gson ();
    private Class< T > clazz;
    private static final String SET_COOKIE_KEY = "set-cookie";

    private static final String IF_NOT_MATCHES = "If-None-Match";

    private String update_at;

    public GsonRequest ( int method, String url, T requestCls, Class< T > responseCls, Response.Listener< T > listener, Response.ErrorListener errorListener ) {

        super ( method, url, mGson.toJson ( requestCls ), listener, errorListener );

        Log.i ( "Gson", "body:" + mGson.toJson ( requestCls ) );
        Log.i ( "URL", "url:" + url );

        this.clazz = responseCls;
    }

    public GsonRequest ( int method, String url, String requestParams, Class< T > responseCls, Response.Listener< T > listener, Response.ErrorListener errorListener ) {

        super ( method, url, requestParams, listener, errorListener );

        this.clazz = responseCls;
    }

    public GsonRequest ( String url, Class< T > responseCls, Response.Listener< T > listener, Response.ErrorListener errorListener ) {

        super ( url, null, listener, errorListener );
        Log.i ( "URL", "url:" + url );

        this.clazz = responseCls;
    }

    public GsonRequest ( int method, String url, Class< T > responseCls, Response.Listener< T > listener, Response.ErrorListener errorListener ) {

        super ( method, url, null, listener, errorListener );
        Log.i ( "URL", "url:" + url );

        this.clazz = responseCls;
    }


    @Override
    protected Response< T > parseNetworkResponse ( NetworkResponse response ) {
        try {

            Map< String, String > headers = response.headers;

            if ( headers == null
                    || headers.equals ( Collections.emptyMap () ) ) {
                headers = new HashMap< String, String > ();

            }

            if ( headers.containsKey ( SET_COOKIE_KEY ) ) {
                String cookie = headers.get ( SET_COOKIE_KEY );
                if ( cookie.length () > 0 ) {

                    setGetCookie ( cookie );
                }
            } else {
                Log.i ( "MyCookie", "false" );
            }

            String json = new String ( response.data, "UTF-8" );
//            String json = new String(
//                    response.data, HttpHeaderParser.parseCharset(response.headers));
            Log.i ( "GetValue", "Value:" + json );

            return Response.success (
                    mGson.fromJson ( json, clazz ), HttpHeaderParser.parseCacheHeaders ( response ) );
        } catch ( UnsupportedEncodingException e ) {
            return Response.error ( new ParseError ( e ) );
        } catch ( JsonSyntaxException e ) {
            return Response.error ( new ParseError ( e ) );
        }
    }

    @Override
    public Map< String, String > getHeaders () throws AuthFailureError {
        Map< String, String > headers = new HashMap< String, String > ( super.getHeaders () );
//        if ( getCookies () != null ) {
//            Log.i("Cookie","add:"+getCookies ());
//            headers.put ( "Cookie",getCookies () );
//        }
        if ( getUpdate_at () != null ) {
            headers.put ( IF_NOT_MATCHES, getUpdate_at () );
        }
        return headers;
    }

    public String getUpdate_at () {
        return update_at;
    }

    public void setUpdate_at ( String update_at ) {
        this.update_at = update_at;
    }
}
